<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Attribute as attributeModel;
use app\admin\validate\Attribute as attributeValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Attribute extends Base{
    // 列表
    public function index(){
        $keyword = input('keyword'); $moduletable = input('moduletable'); $order = ['sequence'=>'desc','id'=>'desc'];
        
        // 转为小写字母
        $tabledir = input('tabledir');
        $general = new \common\General();
        $tabledir = $general->capital_to_underline($tabledir);
        $moduletable = $general->capital_to_underline($moduletable);

        $template = $moduleTitle = '';

        if($moduletable == 'shop_spec'){
            $template = 'spec_index';
            $moduleTitle = lang('v_spec');
            $rs = attributeModel::index('shop_spec',$this->lang,0,$keyword,$order);
        }else{
            $rs = attributeModel::index($tabledir,$this->lang,0,$keyword,$order);
        }

		View::assign('list',$rs);
		View::assign('total',$rs->total());
		View::assign('lastPage',$rs->lastPage());

        // 所属表
        $tableArr = array('ad','chat','feedback','navigation','send_template','shop_aftersale');
        if(in_array($tabledir,$tableArr)){
            $template = 'global_index';
            $langSign = 'v_'.$tabledir;
            $moduleTitle = lang($langSign);
        }
        View::assign('module_title',$moduleTitle);

        // 导航文字
        switch ($tabledir) {
            case 'shop_aftersale':
                $att_nav = lang('v_data_manage');
                break;
            default:
                $att_nav = lang('g_system').' '.lang('v_setting');
                break;
        }
        View::assign('att_nav',$att_nav);
		
        return view($template);
    }
	
    // 添加编辑
    public function edit($id=0){
    	$moduletable = input('moduletable');

        // 转为小写字母
        $tabledir = input('tabledir');
        $general = new \common\General();
        $tabledir = $general->capital_to_underline($tabledir);
        $moduletable = $general->capital_to_underline($moduletable);

        // 导航文字
        switch ($tabledir) {
            case 'shop_aftersale':
                $att_nav = lang('v_data_manage');
                break;
            default:
                $att_nav = lang('g_system').' '.lang('v_setting');
                break;
        }
        View::assign('att_nav',$att_nav);

        // 所属表
        $template = $moduleTitle = '';
        $tableArr = array('ad','chat','feedback','navigation','send_template','shop_spec','shop_aftersale');
        if(in_array($tabledir,$tableArr)){
            $template = 'global_edit';
            $langSign = 'v_'.$tabledir;
            $moduleTitle = lang($langSign);
        }
        if($moduletable == 'shop_spec'){
            $template = 'spec_edit';
            $moduleTitle = lang('v_spec');
        }
        View::assign('module_title',$moduleTitle);

        if(!request()->isPost()){
            $rs = attributeModel::find($id);
            View::assign('rs',$rs);

            if(!in_array($tabledir,$tableArr) and $moduletable != 'shop_spec'){//模块表
                // 子属性
                $sub_attribute = array();
                if($id > 0) $sub_attribute = Db::name('attribute')->where('parentid',$id)->order('sequence desc,id desc')->select();
                View::assign('sub_attribute',$sub_attribute);
                View::assign('more_num',count($sub_attribute));
            }
            
            return view($template);
        }else{
            // 表单验证
            try {
                validate(attributeValidate::class)->check(input('post.'));
            } catch (ValidateException $e) {
                return json(['code' => 1001, 'msg' => $e->getMessage(), 'data' => '']);
            }
            
            try {
                attributeModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }		
	}
	
	// 单条删除
    public function delete_one(){
	    $id = input('id');
        $attribute = attributeModel::find($id);
        // 含有禁止删除的信息
        $nodeleteidArr = explode(',', config('app.no_delete_attid'));
        if(in_array($id, $nodeleteidArr)){
            return json(['code' => 1001, 'msg' => lang('c_nodeleteid_error'), 'data' => array('id'=>$id)]);
        }
        
        try {
            // 删除子规格
            Db::name('attribute')->where('parentid',$id)->delete();
            // 执行删除
            $attribute->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	// 批量删除
    public function delete_all(){
		$selectid = input('selectid/a');
		if(empty($selectid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }

        // 含有模块禁止删除的信息
        $nodeleteidArr = explode(',', config('app.no_delete_attid'));
        foreach ($selectid as $infoid) {
            if(in_array($infoid, $nodeleteidArr)){
                return json(['code' => 1002, 'msg' => lang('c_nodeleteid_error'), 'data' => array('infoid'=>$infoid)]);
            }
            break;
        }
        
        try {
            // 删除子规格
            Db::name('attribute')->where('parentid','in',$selectid)->delete();
            Db::name('attribute')->delete($selectid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	//批量更新
    public function update_all(){
		$id = input('id/a'); $title = input('title/a'); $sequence = input('sequence/a');
        
		foreach($id as $i => $rs){
            $data[] = ['id'=>$id[$i],'title'=>$title[$i],'sequence'=>$sequence[$i]];
        }

        try {
            $attribute = new attributeModel;
            $attribute->saveAll($data,true);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

    //ajax属性
    public function getattr(){
        $id = input('id'); $attributeid = input('attributeid');
        
        // 转为小写字母
        $tabledir = input('tabledir');
        $general = new \common\General();
        $tabledir = $general->capital_to_underline($tabledir);

        //属性列表
        $list = array();
        if(!empty($attributeid) and $attributeid != 0){
            $attWhere[] = ['tabledir','=',$tabledir]; $attWhere[] = ['parentid','=',$attributeid]; $attWhere[] = ['lang','=',$this->lang];
            $list = Db::name('attribute')->where($attWhere)->order('sequence desc,id desc')->select();
        }
        View::assign("list", $list);

        //模块属性值
        $rs_attvalue = array();
        $rs = Db::name($tabledir)->where('id',$id)->find();
        if(!empty($rs) and !empty($rs['attvalue'])) $rs_attvalue = json_decode($rs['attvalue'],true);
        $rs_attvalue = array_column($rs_attvalue,'attvalue');
        View::assign("rs_attvalue",$rs_attvalue);
        //print_r($rs_attvalue);die;

        return view();
    }
}